package tk0084.dev.google.quiz.lite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataSource {
	private static ThreadLocal<Connection> _connection = new ThreadLocal<Connection>() {
		protected Connection initialValue() {
			try {
				Class.forName("org.h2.Driver");
				return DriverManager.getConnection("jdbc:h2:file:src/main/resources/db/db", "sa", "");
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("error initilizeing datasource");
			}
		}
	};

	public static Connection getConnection() {
		return _connection.get();
	}

	public static void close() throws SQLException {
		if (!_connection.get().isClosed()) {
			_connection.get().close();
		}
	}
}
